<?php

namespace App\Http\Controllers\System;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB,Debugbar;

class SContractController extends Controller
{
    public function index()
    {
        // //递归方法把对象转换为数组
        // function objectToArray($e){
        //     $e=(array)$e;
        //     foreach($e as $k=>$v){
        //         if( gettype($v)=='resource' ) return;
        //         if( gettype($v)=='object' || gettype($v)=='array' )
        //             $e[$k]=(array)objectToArray($v);
        //     }
        //     return $e;
        // }
        // //
        // $pact=DB::table('pact')->where('Type',0)->get();
        // foreach($pact as $val)
        // {
        //     $pacttype['pacttype']=objectToArray(DB::table('pacttype')->where('PactId',$val->id)->orderBy('Inx','desc')->get());
        //     $pact_list[]=array_merge(objectToArray($val),$pacttype);
        // }
        // return view('system.sContract',[
        //     'pact_list'=>$pact_list
        //     ]);
        $first_pact=DB::table('pact')->where('Type',0)->first();
        $first_pacttype=DB::table('pacttype')->where('PactId',$first_pact->id)->orderBy('Inx','desc')->get();
        $validity=explode(',',$first_pact->Validity);
        return view('system.sContract',[
             'first_pact'=>$first_pact,
             'first_pacttype'=>$first_pacttype,
             'validity'=>$validity,
             'c_pactList'=>DB::table('pact')->where('Type',0)->get(),
             's_pactList'=>DB::table('pact')->where('Type','>',0)->get()
            ]);
    }

    public function create(Request $request)
    {
        $data=unserialize($request->input('data'));
        //合同名称查重/不为空
        if(trim($data['pact']['name'])==NULL)//不能为空
        {
            return redirect('system/sContract/index')->with('callback', '合同名不能为空');
        }
        $check_name=DB::table('pact')->where('Name',trim($data['pact']['name']))->get();
        if($check_name!=NULL)//查重
        {
            return redirect('system/sContract/index')->with('callback', '合同名重复');
        }
        //pact写入开始
        $pact_insert=array('UseId'=>session('user')[0]->id,'Name'=>$data['pact']['name'],'Type'=>$data['pact']['pact_id'],'Validity'=>DB::table('pact')->where('id',$data['pact']['pact_id'])->pluck('Validity'));
        $pactId=DB::table('pact')->insertGetId($pact_insert);
        //pact写入结束
        //suppacttype写入开始
        //UED价格
        $ued=(array_filter($data['pact_type'], function($var){ return $var[0] == '2'; }));
        if($ued!=NULL)
        {
            foreach($ued as $val)
            {
                $ued_insert[]=array_merge(array_combine(['Type','PactTypeId','UedPrice'], $val),['PactId'=>$pactId]);
            }
            DB::table('suppacttype')->insert($ued_insert);
        }
        //开发价格
        $dev=(array_filter($data['pact_type'], function($var){ return $var[0] == '4'; }));
        if($dev!=NULL)
        {
            foreach($dev as $val)
            {
                $dev_insert[]=array_merge(array_combine(['Type','PactTypeId','KPrice'], $val),['PactId'=>$pactId]);
            }
            DB::table('suppacttype')->insert($dev_insert);
        }
        //套餐价格
        $combo=(array_filter($data['pact_type'], function($var){ return $var[0] == '6'; }));
        if($combo!=NULL)
        {
            foreach($combo as $val)
            {
                $combo_insert[]=array_merge(array_combine(['Type','PactTypeId','UedPrice','KPrice'], $val),['PactId'=>$pactId]);
            }
            DB::table('suppacttype')->insert($combo_insert);
        }
        //suppacttype写入结束
        return redirect('system/sContract/index')->with('callback', '添加成功');
    }

    public function edit(Request $request)
    {
        $pact_id=$request->input('pact_id');
        $data['Pact']=DB::table('pact')->select('id','Name','Validity','Del')->where('id',$request->input('pact_id'))->get();
        $data['SupPactType']=DB::table('suppacttype')
        ->where('suppacttype.PactId','=',$pact_id)
        ->leftjoin('pacttype','suppacttype.PactTypeId','=','pacttype.id')
        ->select('suppacttype.id','suppacttype.Type','suppacttype.UedPrice as SupUedPrice','suppacttype.KPrice as SupKPrice','pacttype.UedWorktime','pacttype.UedPrice','pacttype.UedCommission','pacttype.KWorktime','pacttype.KPrice','pacttype.KCommission','pacttype.Del','pacttype.Inx','pacttype.Name')
        ->orderBy('pacttype.Inx','desc')->get();
        // $data['SupPactType']=DB::select('select `suppacttype`.`id`, `suppacttype`.`Type`, `suppacttype`.`UedPrice` as `SupUedPrice`, `suppacttype`.`KPrice` as `SupKPrice`, `pacttype`.`UedWorktime`, `pacttype`.`UedPrice`, `pacttype`.`UedCommission`, `pacttype`.`KWorktime`, `pacttype`.`KPrice`, `pacttype`.`KCommission`, `pacttype`.`Del`, `pacttype`.`Inx`, `pacttype`.`Name` from `suppacttype` left join `pacttype` on `suppacttype`.`PactTypeId` = `pacttype`.`id` and `suppacttype`.`PactId` ="'.$request->input('pact_id').'" order by `pacttype`.`Inx` desc');
        return json_encode($data);
    }

    public function edit_form(Request $request)
    {
        $data=unserialize($request->input('data'));
        //合同名称不为空
        if(trim($data['pact']['name'])==NULL)//不能为空
        {
            return redirect('system/sContract/index')->with('callback', '合同名不能为空');
        }
        //pact写入开始
        $pact_insert=array('UseId'=>session('user')[0]->id,'Name'=>$data['pact']['name']);
        $pactId=DB::table('pact')->where('id',$request->input('pact_id'))->update($pact_insert);
        //pact写入结束
        //suppacttype写入开始
        //UED价格
        $ued=(array_filter($data['pact_type'], function($var){ return $var[0] == '2'; }));
        if($ued!=NULL)
        {
            foreach($ued as $key=>$val)
            {
                $ued_insert=array_combine(['Type','UedPrice'], $val);
                DB::table('suppacttype')->where('id',$key)->update($ued_insert);
            }
        }
        //开发价格
        $dev=(array_filter($data['pact_type'], function($var){ return $var[0] == '4'; }));
        if($dev!=NULL)
        {
            foreach($dev as $key=>$val)
            {
                $dev_insert=array_combine(['Type','KPrice'], $val);
                DB::table('suppacttype')->where('id',$key)->update($dev_insert);
            }
        }
        //套餐价格
        $combo=(array_filter($data['pact_type'], function($var){ return $var[0] == '6'; }));
        if($combo!=NULL)
        {
            foreach($combo as $key=>$val)
            {
                $combo_insert=array_combine(['Type','UedPrice','KPrice'], $val);
                DB::table('suppacttype')->where('id',$key)->update($combo_insert);
            }
        }
        //suppacttype写入结束
        return redirect('system/sContract/index')->with('callback', '修改成功');
    }

    public function update(Request $request)
    {
        //
    }

    //启用/停用供应商合同
    public function destroy(Request $request)
    {
        if($request->input('pact_del')==1)
        {
            DB::table('pact')->where('id',$request->input('pact_id'))->update(['Del'=>0]);
            //DB::table('suppacttype')->where('PactId',$request->input('pact_id'))->update(['Del'=>0]);
            return 1;
        }
        else
        {
            DB::table('pact')->where('id',$request->input('pact_id'))->update(['Del'=>1]);
            //DB::table('suppacttype')->where('PactId',$request->input('pact_id'))->update(['Del'=>1]);
            return 1;
        }
    }

    public function test()
    {
        
    }
}
